package frame.ddd.lessmodel;

import frame.ddd.lossmodel.basic.*;

import java.io.Serializable;
import java.util.Date;

/**
 * desc     : 竞标这个业务逻辑被放入到Item中来。请注意this.getBids.add(newBid); 如果没有Hibernate或者JDO这种O/R Mapping
 * 的支持，我们是无法实现这种透明的持久化行为的。但是请注意，Item里面不能去调用ItemDAO，对ItemDAO产生依赖！
 * dateTime : 2020/8/6 19:23
 *
 * @author cww
 */

public class Item implements Serializable {
    private Bid getBids;
    private ItemState state;
    //   所有的属性和getter/setter方法同上，省略
    public Bid placeBid(User bidder, MonetaryAmount bidAmount,
                        Bid currentMaxBid, Bid currentMinBid)
            throws BusinessException {

        // Check highest bid (can also be a different Strategy (pattern))
        if (currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0) {
            throw new BusinessException("Bid too low.");
        }

        // Auction is active
        if ( !state.equals(ItemState.ACTIVE) )
            throw new BusinessException("Auction is not active yet.");

        // Auction still valid
        if ( this.getEndDate().before( new Date() ) )
            throw new BusinessException("Can't place new bid, auction already ended.");

        // Create new Bid
        Bid newBid = new Bid(bidAmount, this, bidder);

        // Place bid for this Item
        this.getBids.add(newBid);   // 请注意这一句，透明的进行了持久化，但是不能在这里调用ItemDao，Item不能对ItemDao产生依赖！return newBid;
        return null;
    }
    private Date getEndDate(){
        return null;
    }

}
